package org.game.netty.oldcoder.other;

import com.sojoys.artifact.factory.message.IMessage;

import io.netty.buffer.ByteBuf;

/**
 * @author : DengYing
 * @CreateDate : 2017年10月18日 下午5:57:14
 * @Description ：Please describe this document
 */
public class HuanjieMessage extends IMessage{

	@Override
	public void parse(Object data) {
		ByteBuf in = (ByteBuf) data;
		short flag = in.readShort();
		int length = in.readInt();
		
		// 校验和
		short checksum = in.readShort();
		int checksumContentLength = length-ProtocolConstants.CHECKSUM_SKIP_SIZE_FOR_CONTENT;
		int bodyLength = length - ProtocolConstants.DEFAULT_HEADER_SIZE;
		short calculateCheckSum = CheckSumUtil.calculate(in.nioBuffer(),0,checksumContentLength);
		
		if (calculateCheckSum != checksum) {
			// 跳过当前指令数据内容
			in.skipBytes(checksumContentLength);
				
			
		}
		
		// 指令号
		short messageId = in.readShort();
		if (in.isReadable()) {
			// 验证校验和是否有效
			byte[] body = new byte[bodyLength];
			in.readBytes(body);
			setBody(body);
		}
		setLength(bodyLength);
		setCommandId(messageId);
	}

}
